/*
 * @Author: Allan
 * @Date: 2022-04-29 20:18:20
 * @LastEditors: Allan
 * @LastEditTime: 2022-04-30 09:57:39
 * @FilePath: \golangExcel\excel\excel.go
 * @Description:
 *
 * Copyright (c) 2022 by Allan / ganger@whut.edu.cn, All Rights Reserved.
 */
package excel

import (
	"io/ioutil"
	"path/filepath"

	"github.com/tealeg/xlsx"
)

var HeadLine = []string{"序号", "学院", "姓名", "学号", "性别", "人员类别", "身份证号", "联系电话", "在汉居住地址", "居住地所属行政区划", "从何地（行政区划）返（来）汉及返（来）汉时间", "抵汉时间", "乘坐交通工具时间及具体航班/车次等信息（自驾填写车牌号）", "是否有发热等异常", "新冠疫苗已经打了几针", "户籍地址", "备注"}

const RowHeight = 52.35

var HeadStyle = &xlsx.Style{
	Border: xlsx.Border{
		Left:        "thin",
		LeftColor:   "",
		Right:       "thin",
		RightColor:  "",
		Top:         "thin",
		TopColor:    "",
		Bottom:      "thin",
		BottomColor: "",
	},
	Fill: xlsx.Fill{
		PatternType: "none",
		BgColor:     "",
		FgColor:     "",
	},
	Font: xlsx.Font{
		Size:      12,
		Name:      "黑体",
		Family:    3,
		Charset:   134,
		Color:     "FF000000",
		Bold:      false,
		Italic:    false,
		Underline: false,
	},
	ApplyBorder:    true,
	ApplyFill:      false,
	ApplyFont:      true,
	ApplyAlignment: true,
	Alignment: xlsx.Alignment{
		Horizontal:   "center",
		Indent:       0,
		ShrinkToFit:  false,
		TextRotation: 0,
		Vertical:     "center",
		WrapText:     true,
	},
	// NamedStyleIndex: (*int)(0xc0000f99f8),
}

func SetHeadLine(sheet *xlsx.Sheet) {

	// style := xlsx.NewStyle()
	// style.Font =

	row := sheet.AddRow()

	// 设置行高
	row.SetHeight(RowHeight)

	var cell *xlsx.Cell
	for _, item := range HeadLine {
		cell = row.AddCell()
		cell.SetStyle(HeadStyle)
		cell.Value = item
	}

}

func GetAllFiles(dirPth string) (files []string, err error) {
	fis, err := ioutil.ReadDir(filepath.Clean(filepath.ToSlash(dirPth)))
	if err != nil {
		return nil, err
	}

	for _, f := range fis {
		_path := filepath.Join(dirPth, f.Name())

		if f.IsDir() {
			fs, _ := GetAllFiles(_path)
			files = append(files, fs...)
			continue
		}

		// 指定格式
		switch filepath.Ext(f.Name()) {
		// case ".png", ".jpg":
		case ".xlsx":
			n := f.Name()
			if n[0] == '~' {
				continue
			}
			// files = append(files, _path)
			files = append(files, f.Name())
		}
	}

	return files, nil
}

// 复制一行
func CopyRow(target, source *xlsx.Row) {

	target.SetHeight(source.Height)

	for _, cl := range source.Cells {
		cel := target.AddCell()
		cel.Value = cl.Value
		cel.HMerge = cl.HMerge
		cel.VMerge = cl.VMerge
		cel.SetStyle(cl.GetStyle())
	}
}
